package com.syx.server.service;

import java.net.Socket;
import java.util.HashMap;
import java.util.Iterator;

/**
 * @author 邵一鑫
 * @date 2024/1/13 23:04
 * 该类用于管理 server 连接到 client 的 thread
 */
public class ManageConnectClientThread {

    private static HashMap<String, ServerConnectClientThread> threadMap = new HashMap<>();

    /**
     * 添加线程
     * @param userId key
     * @param serverConnectClientThread 服务端连接到客户端的线程
     */
    public static void addServerConnectClientThread(String userId, ServerConnectClientThread serverConnectClientThread) {
        threadMap.put(userId, serverConnectClientThread);
    }

    /**
     * 通过 userId 获取线程
     * @param userId key
     * @return
     */
    public static ServerConnectClientThread getServerConnectClientThread(String userId) {
        return threadMap.get(userId);
    }

    /**
     * 返回在线用户列表
     * @return
     */
    public static String getOnlineUser() {
        Iterator<String> iterator = threadMap.keySet().iterator();
        String onlineUserList = "";
        while (iterator.hasNext()) {
            onlineUserList += iterator.next().toString() + " ";
        }
        return  onlineUserList;
    }

    /**
     * 移除指定 userId 的线程
     * @param userId
     */
    public static void removeThread(String userId){
        threadMap.remove(userId);
    }

    /**
     * 返回当前所有线程的集合
     * @return
     */
    public static HashMap<String, ServerConnectClientThread> getThreadMap() {
        return threadMap;
    }
}
